/*
 * Copyright 2011-2012 University of Illinois
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* ---------------------------------------------------------------- */
/*                  Preferences Sidebar                             */
/* ---------------------------------------------------------------- */

/**
 * @namespace OAA_CI
 */

var OAA_CI = OAA_CI || {};
OAA_CI.preferences = OAA_CI.preferences || {};


OAA_CI.preferences.preferencesView = new OAA_CI.SidebarView('ID_LISTBOX_RULESETS');

OAA_CI.preferences.views = [OAA_CI.preferences.preferencesView];

OAA_CI.preferences.hideMeter = function () {};
OAA_CI.preferences.showMeter = function () {};

OAA_CI.sidebar = OAA_CI.preferences;


/**
 * @function onLoad
 *
 * @memberOf OAA_CI.preferences
 *
 * @desc Initialize sidebar and populate its default tabpanel.
 */

OAA_CI.preferences.onLoad = function () {
 
  this.initControlsFromPrefs();
 
};

/**
 * @function initControlsFromPrefs
 *
 * @memberOf OAA_CI.preferences
 *
 * @desc Initialize controls with current preference values
 */

OAA_CI.preferences.initControlsFromPrefs = function () {

  var i;
  var label;
  
  var pref_manager;
  var ruleset_id;
  var wcag20_level;
  var broken_links;

  var preferences = OAA_CI_UTILS.getPreferences();

  this.rulesets = OpenAjax.a11y.all_rulesets.getAllRulesets();
  
  var ruleset;

  // Ruleset listbox

  var listbox_rulesets = document.getElementById("ID_LISTBOX_RULESETS");
  var listitem_ruleset;
  
  var selected_index = -1;
  
  OAA_CI.removeChildNodes(listbox_rulesets);
  
  for (i = 0; i < this.rulesets.length; i++) {
    ruleset = this.rulesets[i];
  
    listitem_ruleset = document.createElement("listitem");
    listitem_ruleset.setAttribute("label", ruleset.ruleset_title + " (" + ruleset.ruleset_version + ")");
    listitem_ruleset.setAttribute("id",    ruleset.ruleset_id);
  
    if (preferences.ruleset_id == ruleset.ruleset_id) selected_index = i;
  
    listbox_rulesets.appendChild(listitem_ruleset);
  }
  
  listbox_rulesets.selectedIndex = selected_index;
  
  // Radio group for WCAG 2.0 level

  var radiogroup_wcag20_level   = document.getElementById("ID_RADIOGROUP_LEVEL");
  
  if (preferences.wcag20_level >= OpenAjax.a11y.WCAG20_LEVEL.A && 
      preferences.wcag20_level <= OpenAjax.a11y.WCAG20_LEVEL.AAA) {
   
    switch(preferences.wcag20_level) {
    
    case OpenAjax.a11y.WCAG20_LEVEL.AAA:
      radiogroup_wcag20_level.selectedIndex = 0;
      break;
    
    case OpenAjax.a11y.WCAG20_LEVEL.AA:
      radiogroup_wcag20_level.selectedIndex = 1;
      break;

    case OpenAjax.a11y.WCAG20_LEVEL.A:
      radiogroup_wcag20_level.selectedIndex = 2;
      break;
    
    default:
      radiogroup_wcag20_level.selectedIndex = 0;
      break;
    
    } // end switch
  }    
    
  // Broken links

  var checkbox_broken_links = document.getElementById("ID_CHECKBOX_BROKEN_LINKS");
  checkbox_broken_links.checked = preferences.broken_links;

  // Show menu

  var checkbox_show_menu = document.getElementById("ID_CHECKBOX_SHOW_MENU");
  checkbox_show_menu.checked = preferences.show_menu;
  
  var button_save  = document.getElementById("ID_BUTTON_PREFERENCES_SAVE");
  button_save.disabled = true;

};


/**
 * @function updatePrefsFromControlValues
 *
 * @memberOf OAA_CI.preferences
 *
 * @desc Initialize ruleset listbox
 */

OAA_CI.preferences.updatePrefsFromControlValues = function () {

  // Ruleset listbox
  
  var preferences = OAA_CI_UTILS.getPreferences();

  var listbox_rulesets = document.getElementById("ID_LISTBOX_RULESETS");
  var listitem_ruleset = listbox_rulesets.selectedItem;

  if (listitem_ruleset && listitem_ruleset.id) {
    preferences.ruleset_id = listitem_ruleset.id;  
  }
  
  // Radio group for WCAG 2.0 level

  var radio_level_a   = document.getElementById("ID_RADIO_LEVEL_A");
  var radio_level_aa  = document.getElementById("ID_RADIO_LEVEL_AA");
  var radio_level_aaa = document.getElementById("ID_RADIO_LEVEL_AAA");
  
  if (radio_level_a.selected)   preferences.wcag20_level = OpenAjax.a11y.WCAG20_LEVEL.A;
  if (radio_level_aa.selected)  preferences.wcag20_level = OpenAjax.a11y.WCAG20_LEVEL.AA;
  if (radio_level_aaa.selected) preferences.wcag20_level = OpenAjax.a11y.WCAG20_LEVEL.AAA;
  
  // Broken links

  var checkbox_broken_links = document.getElementById("ID_CHECKBOX_BROKEN_LINKS");
  preferences.broken_links = checkbox_broken_links.checked;  

  // Show menu

  var checkbox_show_menu = document.getElementById("ID_CHECKBOX_SHOW_MENU");
  preferences.show_menu = checkbox_show_menu.checked;  
  
  OAA_CI_UTILS.setPreferences(preferences);
  
  var button_save  = document.getElementById("ID_BUTTON_PREFERENCES_SAVE");
  button_save.disabled = true;

};


/**
 * @function resetPreferencesToDefaultValues
 *
 * @memberOf OAA_CI.preferences
 *
 * @desc Reset references to default values
 */

OAA_CI.preferences.resetPreferencesToDefaultValues = function () {

  OAA_CI_UTILS.setDefaultPreferences();
  OAA_CI.preferences.initControlsFromPrefs();
};


/**
 * @function preferencesView.showCacheItems
 *
 * @memberOf OAA_CI.preferences
 *
 * @desc Respond to the Tree View button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with list
 *       elements structured as a hierarchical list. Update properties
 *       display and page highlighting based on selection state of panel.
 */

OAA_CI.preferences.preferencesView.showCacheItems = function () {
  // this view does not display cache items
};

/**
 * @function onRulesetSelectionChange
 *
 * @memberOf OAA_CI.preferences
 *
 * @desc Initialize sidebar and populate its default tabpanel.
 */

OAA_CI.preferences.onItemSelectionChange = function () {

  var i;

  var listbox_rulesets    = document.getElementById("ID_LISTBOX_RULESETS");

  var textbox_description = document.getElementById("ID_TEXTBOX_DESCRIPTION");

  var textbox_author      = document.getElementById("ID_TEXTBOX_AUTHOR");

  var ruleset_item = listbox_rulesets.selectedItem;


  var ruleset = null;
  
  if (ruleset_item) {
    for (i = 0; i < this.rulesets.length; i++) {
      if (ruleset_item.id == this.rulesets[i].ruleset_id) {
        ruleset = this.rulesets[i];
        break;
      }
    } // end loop  
  }

  if (ruleset) {
    textbox_description.value = ruleset.ruleset_description;
    textbox_author.value      = ruleset.author_name;
  }
  else {
    textbox_description.value = "no description";
    textbox_author.value      = "no author";
  }

  var button_save  = document.getElementById("ID_BUTTON_PREFERENCES_SAVE");
  button_save.disabled = false;
  
};
